# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def check_window(min_est, max_est, min_event_v, max_event_v, leadlag_size)
    min_event = "#{min_event_v.to_i+leadlag_size.to_i}"
    max_event = "#{max_event_v.to_i-leadlag_size.to_i}"
    error_count = 0
    begin
      if min_est.empty?
        error_count += 1
      end
      if max_est.empty?
        error_count += 1
      end
      if min_event.empty?
        error_count += 1
      end
      if max_event.empty?
        error_count += 1
      end
      if min_est.to_i>0 || max_est.to_i>0
        error_count += 1
      end
      if min_est.to_i > max_est.to_i
        error_count += 1
      end
      if min_event.to_i>0 || max_event.to_i<0
        error_count += 1
      end
      if max_est.to_i+1 != min_event.to_i
        error_count += 1
      end
    rescue
      error_count += 1
    end
    if error_count==0
      return true
    else
      return false
    end
  end

  def check_tradedate(id, stock_type, event_date)
    st = nil
    if stock_type=='S'
      stock = Stock.find(:first, :conditions=>["id=?",id])
      if stock
        st = StockTrade.find(:first, :conditions=>["stock_id=? AND trade_date=? AND daily_return IS NOT NULL",stock.id,event_date], :order=>"id")
      end
    else
      stock = UserStock.find(:first, :conditions=>["id=?",id])
      if stock
        st = UserStockTrade.find(:first, :conditions=>["user_stock_id=? AND trade_date=? AND daily_return IS NOT NULL",stock.id,event_date], :order=>"id")
      end
    end
    if st
      return true
    else
      return false
    end
  end

  def get_minmax_tradedate(id, stock_type)
    min_date = "-"
    max_date = "-"
    if stock_type=='S'
      stock = Stock.find(:first, :conditions=>["id=?",id])
      if stock
        min = StockTrade.find(:first, :conditions=>["stock_id=?",stock.id], :order=>"trade_date")
        min = StockTrade.find(:first, :conditions=>["stock_id=? AND id!=?",stock.id,min.id], :order=>"trade_date")
        if min
          min_date = "#{min.trade_date}"
        end
        max = StockTrade.find(:last, :conditions=>["stock_id=?",stock.id], :order=>"trade_date")
        if max
          max_date = "#{max.trade_date}"
        end
      end
    else
      stock = UserStock.find(:first, :conditions=>["id=?",id])
      if stock
        min = UserStockTrade.find(:first, :conditions=>["user_stock_id=?",stock.id], :order=>"trade_date")
        min = UserStockTrade.find(:first, :conditions=>["user_stock_id=? AND id!=?",stock.id,min.id], :order=>"trade_date")
        if min
          min_date = "#{min.trade_date}"
        end
        max = UserStockTrade.find(:last, :conditions=>["user_stock_id=?",stock.id], :order=>"trade_date")
        if max
          max_date = "#{max.trade_date}"
        end
      end
    end
    return min_date,max_date
  end

  def get_minmax_year(id, stock_type)
    min_year = 1900
    max_year = 2999
    if stock_type=='S'
      stock = Stock.find(:first, :conditions=>["id=?",id])
      if stock
        min = StockTrade.find(:first, :conditions=>["stock_id=?",stock.id], :order=>"trade_date")
        min = StockTrade.find(:first, :conditions=>["stock_id=? AND id!=?",stock.id,min.id], :order=>"trade_date")
        if min
          min_year = min.trade_date.year
        end
        max = StockTrade.find(:last, :conditions=>["stock_id=?",stock.id], :order=>"trade_date")
        if max
          max_year = max.trade_date.year
        end
      end
    else
      stock = UserStock.find(:first, :conditions=>["id=?",id])
      if stock
        min = UserStockTrade.find(:first, :conditions=>["user_stock_id=?",stock.id], :order=>"trade_date")
        min = UserStockTrade.find(:first, :conditions=>["user_stock_id=? AND id!=?",stock.id,min.id], :order=>"trade_date")
        if min
          min_year = min.trade_date.year
        end
        max = UserStockTrade.find(:last, :conditions=>["user_stock_id=?",stock.id], :order=>"trade_date")
        if max
          max_year = max.trade_date.year
        end
      end
    end
    return min_year,max_year
  end

  def check_est_event_pass(stock, stock_type, event_date, min_est, max_est, min_event, max_event)
    pass = [false,false,false,false]
    label = ['X','X','X','X']
    min_est_date = nil
    max_est_date = nil
    min_event_date = nil
    max_event_date = nil
    if stock_type=='S'
      st = StockTrade.find(:all, :conditions=>["stock_id=? AND trade_date<=?",stock.id,event_date], :order=>"trade_date desc")
      label[0] = st.last(2)[0].trade_date
      min_est_date = st[min_est.to_i * -1]
      max_est_date = st[max_est.to_i * -1]
      min_event_date = st[min_event.to_i * -1]
      st = StockTrade.find(:all, :conditions=>["stock_id=? AND trade_date>=?",stock.id,event_date], :order=>"trade_date")
      label[1] = st.last.trade_date
      max_event_date = st[max_event.to_i]
    else
      st = UserStockTrade.find(:all, :conditions=>["user_stock_id=? AND trade_date<=?",stock.id,event_date], :order=>"trade_date desc")
      label[0] = st.last(2)[0].trade_date
      min_est_date = st[min_est.to_i * -1]
      max_est_date = st[max_est.to_i * -1]
      min_event_date = st[min_event.to_i * -1]
      st = UserStockTrade.find(:all, :conditions=>["user_stock_id=? AND trade_date>=?",stock.id,event_date], :order=>"trade_date")
      label[1] = st.last.trade_date
      max_event_date = st[max_event.to_i]
    end

    if min_est_date
      if min_est_date.daily_return
        pass[0] = true
      end
      label[2] = "#{min_est_date.trade_date}"
    end
    if max_est_date
      if max_est_date.daily_return
        pass[1] = true
      end
    end
    if min_event_date
      if min_event_date.daily_return
        pass[2] = true
      end
    end
    if max_event_date
      if max_event_date.daily_return
        pass[3] = true
      end
      label[3] = "#{max_event_date.trade_date}"
    end
    return pass,label
  end

  def show_checkchart(pass,label,no)
    word = "<span id='chartdiv_#{no}' align='center'></span>\n"
    word += "<script type='text/javascript'>\n"
    word += "var myChart = new FusionCharts('/Charts/HLinearGauge.swf', 'myChartId#{no}', '280', '55', '0', '0');\n"
    word += "myChart.setDataXML('#{get_checkchart_xml(pass,label)}');\n"
    word += "myChart.render('chartdiv_#{no}');\n"
    word += "</script>"
    return word
  end

  def get_checkchart_xml(pass,label)
    if ((pass[0]==true && pass[1]==true) && pass[2]==true) && pass[3]==true
      return checkchart_xml_type1(label)
    elsif (pass[0]==false || pass[1]==false) && (pass[2]==true && pass[3]==true)
      return checkchart_xml_type2(label)
    elsif (pass[0]==true && pass[1]==true) && (pass[2]==false || pass[3]==false)
      return checkchart_xml_type3(label)
    else
      return checkchart_xml_type4(label)
    end
  end

  def checkchart_xml_type1(label)
    xml = "<Chart showTickMarks='0' showTickValues='0' bgColor='FFFFFF' bgAlpha='0' showBorder='0' upperLimit='120' lowerLimit='0' gaugeRoundRadius='5' chartTopMargin='0' chartBottomMargin='0' chartLeftMargin='40' chartRightMargin='40' ticksBelowGauge='0' showGaugeLabels='1' valueAbovePointer='0' pointerOnTop='1' pointerRadius='9'>"
    xml += "<colorRange>"
    xml += "<color minValue='0' maxValue='14' code='FFFFFF'/>"
    xml += "<color minValue='14' maxValue='74' code='5C8F0E' label='Estimation'/>"
    xml += "<color minValue='74' maxValue='75' code='FFFFFF'/>"
    xml += "<color minValue='75' maxValue='106' code='5C8F0E' label='Event'/>"
    xml += "<color minValue='106' maxValue='120' code='FFFFFF' />"
    xml += "</colorRange>"
    xml += "<trendpoints>"
    xml += "<point value='91' showOnTop='0' displayValue='E' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='2'/>"
    xml += "<point value='0' showOnTop='0' displayValue='#{label[0]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='120' showOnTop='0' displayValue='#{label[1]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='14' showOnTop='1' displayValue='#{label[2]}' dashed='1' dashLen='1' dashGap='3' color='5C8F0E' thickness='0'/>"
    xml += "<point value='106' showOnTop='1' displayValue='#{label[3]}' dashed='1' dashLen='1' dashGap='3' color='5C8F0E' thickness='0'/>"
    xml += "</trendpoints>"
    xml += "<styles>"
    xml += "<definition>"
    xml += "<style name='limitFont' type='Font' bold='1'/>"
    xml += "<style name='labelFont' type='Font' bold='1' size='10' color='FFFFFF'/>"
    xml += "</definition>"
    xml += "<application>"
    xml += "<apply toObject='GAUGELABELS' styles='labelFont'/>"
    xml += "<apply toObject='LIMITVALUES' styles='limitFont'/>"
    xml += "</application>"
    xml += "</styles>"
    xml += "</Chart>"
    return xml
  end

  def checkchart_xml_type2(label)
    xml = "<Chart showTickMarks='0' showTickValues='0' bgColor='FFFFFF' bgAlpha='0' showBorder='0' upperLimit='120' lowerLimit='0' gaugeRoundRadius='5' chartTopMargin='0' chartBottomMargin='0' chartLeftMargin='40' chartRightMargin='40' ticksBelowGauge='0' showGaugeLabels='1' valueAbovePointer='0' pointerOnTop='1' pointerRadius='9'>"
    xml += "<colorRange>"
    xml += "<color minValue='0' maxValue='50' code='B40001' label='Estimation'/>"
    xml += "<color minValue='50' maxValue='51' code='FFFFFF'/>"
    xml += "<color minValue='51' maxValue='82' code='5C8F0E' label='Event'/>"
    xml += "<color minValue='82' maxValue='120' code='FFFFFF'/>"
    xml += "</colorRange>"
    xml += "<trendpoints>"
    xml += "<point value='66' showOnTop='0' displayValue='E' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='2'/>"
    xml += "<point value='0' showOnTop='0' displayValue='#{label[0]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='120' showOnTop='0' displayValue='#{label[1]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='0' showOnTop='1' displayValue='#{label[2]}' dashed='1' dashLen='1' dashGap='3' color='B40001' thickness='0'/>"
    xml += "<point value='82' showOnTop='1' displayValue='#{label[3]}' dashed='1' dashLen='1' dashGap='3' color='5C8F0E' thickness='0'/>"
    xml += "</trendpoints>"
    xml += "<styles>"
    xml += "<definition>"
    xml += "<style name='limitFont' type='Font' bold='1'/>"
    xml += "<style name='labelFont' type='Font' bold='1' size='10' color='FFFFFF'/>"
    xml += "</definition>"
    xml += "<application>"
    xml += "<apply toObject='GAUGELABELS' styles='labelFont'/>"
    xml += "<apply toObject='LIMITVALUES' styles='limitFont'/>"
    xml += "</application>"
    xml += "</styles>"
    xml += "</Chart>"
    return xml
  end

  def checkchart_xml_type3(label)
    xml = "<Chart showTickMarks='0' showTickValues='0' bgColor='FFFFFF' bgAlpha='0' showBorder='0' upperLimit='120' lowerLimit='0' gaugeRoundRadius='5' chartTopMargin='0' chartBottomMargin='0' chartLeftMargin='40' chartRightMargin='40' ticksBelowGauge='0' showGaugeLabels='1' valueAbovePointer='0' pointerOnTop='1' pointerRadius='9'>"
    xml += "<colorRange>"
    xml += "<color minValue='0' maxValue='33' code='FFFFFF'/>"
    xml += "<color minValue='33' maxValue='93' code='5C8F0E' label='Estimation'/>"
    xml += "<color minValue='93' maxValue='94' code='FFFFFF'/>"
    xml += "<color minValue='94' maxValue='120' code='B40001' label=' Event'/>"
    xml += "</colorRange>"
    xml += "<trendpoints>"
    xml += "<point value='110' showOnTop='0' displayValue='.' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='2'/>"
    xml += "<point value='0' showOnTop='0' displayValue='#{label[0]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='120' showOnTop='0' displayValue='#{label[1]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='33' showOnTop='1' displayValue='#{label[2]}' dashed='1' dashLen='1' dashGap='3' color='5C8F0E' thickness='0'/>"
    xml += "<point value='120' showOnTop='1' displayValue='#{label[3]}' dashed='1' dashLen='1' dashGap='3' color='B40001' thickness='0'/>"
    xml += "</trendpoints>"
    xml += "<styles>"
    xml += "<definition>"
    xml += "<style name='limitFont' type='Font' bold='1'/>"
    xml += "<style name='labelFont' type='Font' bold='1' size='10' color='FFFFFF'/>"
    xml += "</definition>"
    xml += "<application>"
    xml += "<apply toObject='GAUGELABELS' styles='labelFont'/>"
    xml += "<apply toObject='LIMITVALUES' styles='limitFont'/>"
    xml += "</application>"
    xml += "</styles>"
    xml += "</Chart>"
    return xml
  end

  def checkchart_xml_type4(label)
    xml = "<Chart showTickMarks='0' showTickValues='0' bgColor='FFFFFF' bgAlpha='0' showBorder='0' upperLimit='120' lowerLimit='0' gaugeRoundRadius='5' chartTopMargin='0' chartBottomMargin='0' chartLeftMargin='40' chartRightMargin='40' ticksBelowGauge='0' showGaugeLabels='1' valueAbovePointer='0' pointerOnTop='1' pointerRadius='9'>"
    xml += "<colorRange>"
    xml += "<color minValue='0' maxValue='85' code='B40001' label='Estimation'/>"
    xml += "<color minValue='86' maxValue='87' code='FFFFFF'/>"
    xml += "<color minValue='87' maxValue='120' code='B40001' label=' Event'/>"
    xml += "</colorRange>"
    xml += "<trendpoints>"
    xml += "<point value='110' showOnTop='0' displayValue='.' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='2'/>"
    xml += "<point value='0' showOnTop='0' displayValue='#{label[0]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='120' showOnTop='0' displayValue='#{label[1]}' dashed='1' dashLen='1' dashGap='3' color='FFFFFF' thickness='0'/>"
    xml += "<point value='0' showOnTop='1' displayValue='#{label[2]}' dashed='1' dashLen='1' dashGap='3' color='B40001' thickness='0'/>"
    xml += "<point value='120' showOnTop='1' displayValue='#{label[3]}' dashed='1' dashLen='1' dashGap='3' color='B40001' thickness='0'/>"
    xml += "</trendpoints>"
    xml += "<styles>"
    xml += "<definition>"
    xml += "<style name='limitFont' type='Font' bold='1'/>"
    xml += "<style name='labelFont' type='Font' bold='1' size='10' color='FFFFFF'/>"
    xml += "</definition>"
    xml += "<application>"
    xml += "<apply toObject='GAUGELABELS' styles='labelFont'/>"
    xml += "<apply toObject='LIMITVALUES' styles='limitFont'/>"
    xml += "</application>"
    xml += "</styles>"
    xml += "</Chart>"
    return xml
  end

  def get_t_test_star(df, v_str)
    t_star = ''
     if df<=0
      t_star = ''
    else
      begin
        v = v_str.to_f
          s = Student.find(:first, :conditions=>["df>=?", df], :order=>"df")
        if (v>=s.p01) || (v<=s.p01*-1)
          t_star = '***'
        elsif (v>=s.p05) || (v<=s.p05*-1)
          t_star = '**'
        elsif (v>=s.p10) || (v<=s.p10*-1)
          t_star = '*'
        else
          t_star = ''
        end
      rescue
        t_star = ''
      end
    end
    return "<font color='red'>#{t_star}</font>"
  end

end
